never overwrite a file when uploading again

simply pass your upload dir and the filename of the uploaded file, this will check if that filename already exists, if it does, it will add a (n) suffix to the filename, similar to how windows does.

=====================================================

function get_next_filename($dir, $filename) {
  if(!file_exists($dir . $filename)) {
    return $filename;
  }

  $x = 0;
  $fex = explode(".", $filename);
  $newfile = $fex[0] . "({$x})" . "." . $fex[1];

  while(file_exists($dir . $newfile)) {
    $x++;
    $newfile = $fex[0] . "({$x})" . "." . $fex[1];
  }

  return $newfile;
}
